import { defineStore } from 'pinia'
import { getAllBreadcrumbList, getKeepAliveRouterName, getShowMenuList } from '@/utils/util'
import { getAuthButtonListApi, getAuthMenuListApi } from '@/api/modules/login'
import piniaPersistConfig from '@/config/piniaPersist'

export const AuthStore = defineStore('AuthStore', {
  state: () => {
    return {
      authButtonList: {},
      // menuList 作为动态路由，不会做持久化存储
      authMenuList: <Menu.MenuOptions[]>[]
    }
  },
  getters: {
    // 按钮权限列表
    authButtonListGet: (state) => state.authButtonList,
    // 后端返回的菜单列表
    authMenuListGet: (state) => state.authMenuList,
    // 后端返回的菜单列表 ==> 左侧菜单栏渲染，需要去除 isHide == true
    showMenuListGet: (state) => getShowMenuList(state.authMenuList),
    // 面包屑导航列表
    breadcrumbListGet: (state) => getAllBreadcrumbList(state.authMenuList),
    // 需要缓存的菜单 name，用作页面 keepAlive
    keepAliveRouterGet: (state) => getKeepAliveRouterName(state.authMenuList)
  },
  actions: {
    // getAuthButtonList
    async getAuthButtonList() {
      const { data } = await getAuthButtonListApi()
      this.authButtonList = data
    },
    // getAuthMenuList
    async getAuthMenuList() {
      const { data } = await getAuthMenuListApi()
      console.log('data', data)
      this.authMenuList = data
    }
  },
  persist: piniaPersistConfig('AuthStore', ['authButtonList'])
})
